package com.iqiyi.pps.epg.web.utils.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 生成下拉列表
 * sample:
 *  <dict:select entity="Contract" name="contractId" optionValue="id" optionText="name" otherProperty="class='qsbox'" showDefaultOption="true" selectedValue="1"/>
 *  表示从Contract实体中查询id,name字段，生成下拉列表，
 *  下拉列表name属性和id属性均为contractId
 *  下拉列表显示默认值"请选择“
 *  下拉列表的选中值为 1
 * User: wayne
 * Date: 12-6-5
 * Time: 上午11:23
 */
public class SelectTag extends BodyTagSupport {

    private String name; //下拉标签名字
    
    private String style = "";  //样式

    private String entity;  //查询实体Model名字
    
    private String optionValue;   //下拉列表值列，选择entity中的一个属性
    
    private String optionText;   //下拉列表text列，选择entity中的一个属性

    private String selectedValue = ""; //所选中的值

    private String condition = ""; //查询其他条件

    private String otherProperty = "";  //下拉标签的其他属性
    
    private boolean showDefaultOption = true;   //是否显示默认选项

    private String headValue = "";   //默认选项的value

    private String headText = "请选择";   //默认选项的text

    private boolean  dbQuery = true;   //是否从db中查询  暂时只能从db中查询



    @Override
    public int doStartTag() throws JspException {
        try {
            StringBuffer results = new StringBuffer("<select");
            if(name != null){
                results.append(" id=\"");
                results.append(name);
                results.append("\"");

                results.append(" name=\"");
                results.append(name);
                results.append("\"");
            }
            if(style != null &&  !"".equals(style.trim())){
                results.append(" style=\"");
                results.append(style);
                results.append("\"");
            }

            if(otherProperty != null && !"".equals(otherProperty.trim())){
                results.append(" "+otherProperty);
            }
            results.append(">");
            pageContext.getOut().write(results.toString());
        } catch (IOException ex) {
            throw new JspTagException("错误");
        }
        return EVAL_BODY_INCLUDE;
    }
    @Override
    public int doEndTag() throws JspException {
        try {

            StringBuffer results = new StringBuffer("");
            if(isShowDefaultOption()){
               results.append("<option value=\""+headValue+"\">"+ headText +"</option>");
            }
            Map map = getOptionsMap();
            for(Object key:map.keySet()){
                if(selectedValue != null && key.toString().equals(selectedValue)) {
                        results.append("<option value=\""+key+"\"  selected=\"selected\">"+map.get(key)+"</option>");
                } else {
                        results.append("<option value=\""+key+"\">"+map.get(key)+"</option>");
                }
            }
            // 因为下拉中包含下拉内容，所以只能在遇到结束标签时才能写select结束
            results.append("</select>");
            pageContext.getOut().write(results.toString());
        } catch (IOException ex) {
            throw new JspTagException("错误");
        }
        return EVAL_PAGE;
    }

    private Map getOptionsMap(){
        Map map = new HashMap();
        if(dbQuery){
            Map<String,String> conditionsMap = new HashMap<String, String>();
            conditionsMap.put("optionValue",optionValue);
            conditionsMap.put("optionText",optionText);
            conditionsMap.put("entity",entity);
            conditionsMap.put("condition",condition);
//            DictManager dictManager = (DictManager) ApplicationContextUtil.getBean("dictManager");
//            map = dictManager.getMap(conditionsMap,new Object[]{});
        }else {

        }
        return map;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getEntity() {
        return entity;
    }

    public void setEntity(String entity) {
        this.entity = entity;
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

    public boolean isShowDefaultOption() {
        return showDefaultOption;
    }

    public void setShowDefaultOption(boolean showDefaultOption) {
        this.showDefaultOption = showDefaultOption;
    }

    public boolean isDbQuery() {
        return dbQuery;
    }

    public void setDbQuery(boolean dbQuery) {
        this.dbQuery = dbQuery;
    }

    public String getOtherProperty() {
        return otherProperty;
    }

    public void setOtherProperty(String otherProperty) {
        this.otherProperty = otherProperty;
    }


    public String getOptionValue() {
        return optionValue;
    }

    public void setOptionValue(String optionValue) {
        this.optionValue = optionValue;
    }

    public String getOptionText() {
        return optionText;
    }

    public void setOptionText(String optionText) {
        this.optionText = optionText;
    }

    public String getSelectedValue() {
        return selectedValue;
    }

    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }

    public String getHeadText() {
        return headText;
    }

    public void setHeadText(String headText) {
        this.headText = headText;
    }

    public String getHeadValue() {
        return headValue;
    }

    public void setHeadValue(String headValue) {
        this.headValue = headValue;
    }
}
